package web.common.internal.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import web.common.core.security.PasswordEncoder;
import web.common.core.security.PasswordEncoderType;
import web.common.core.security.SecurityFactory;

/**
 * @author Jin Zheng
 * @since 1.0 2022-01-20
 */
@Configuration
@EnableConfigurationProperties(PasswordEncoderProperties.class)
public class PasswordEncoderAutoConfiguration {
	private static Logger logger = LoggerFactory.getLogger(PasswordEncoderAutoConfiguration.class);

	public PasswordEncoderAutoConfiguration() {
	}

	@Bean
	public PasswordEncoder passwordEncoder(PasswordEncoderProperties prop) {
		var type = PasswordEncoderType.getType(prop.getType());
		if (type == null) {
			throw new UnsupportedOperationException("未知类型: " + prop.getType());
		}
		logger.info("Password-Encoder type: {}", type);
		return SecurityFactory.createPasswordEncoder(type);
	}

}
